package com.john.http.core.util;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 〈一句话功能简述〉<br/>
 * 〈功能详细描述〉
 *
 * @author jiangguangtao on 2016/4/28.
 */
public class ThreadUtil {
    private static final BlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<>();
    private static final ThreadPoolExecutor executorService = new ThreadPoolExecutor(4, 4, 0, TimeUnit.SECONDS, taskQueue);

    /**
     * 是否有还活动任务或者未执行的任务
     *
     * @return
     */
    public static boolean hasAnyTasks() {
        return executorService.getActiveCount() > 0 || executorService.getPoolSize() > 0;
    }

    /**
     * 在进行中执行命令
     *
     * @param runnable
     */
    public static void execute(Runnable runnable) {
        executorService.execute(runnable);
    }

    public static Future<?> submit(Runnable task) {
        return executorService.submit(task);
    }

    public static <T> Future<T> submit(Runnable task, T result) {
        return executorService.submit(task, result);
    }

}
